package coins;

//~--- non-JDK imports --------------------------------------------------------

import com.dukascopy.api.IBar;
import com.dukascopy.api.ITick;

//~--- JDK imports ------------------------------------------------------------

import java.util.Date;

public class Bar implements IBar {
    private double close;
    private long   closetime;
    private double high;
    private long   highttime;
    private double low;
    private long   lowtime;
    private double open;
    private long   time;
    private double volume;

    public Bar(long time, double open, double volume) {
        this.time   = time;
        this.open   = open;
        this.high   = open;
        this.low    = open;
        this.close  = open;
        this.volume = volume;
        setHighttime(time);
        setLowtime(time);
    }

    public Bar(long time, double open, double high, double low, double close) {
        this.time  = time;
        this.open  = open;
        this.high  = high;
        this.low   = low;
        this.close = close;
    }

    @Override
    public double getClose() {
        return close;
    }

    @Override
    public double getHigh() {
        return high;
    }

    @Override
    public double getLow() {
        return low;
    }

    @Override
    public double getOpen() {
        return open;
    }

    @Override
    public long getTime() {
        return time;
    }

    @Override
    public double getVolume() {
        return volume;
    }

    public void update(double price, double volume, long time) {
        close = price;
        setClosetime(time);
        this.volume += volume;

        if (price > high) {
            high = price;
            setHighttime(time);
        } else if (price < low) {
            low = price;
            setLowtime(time);
        }
    }

    public void setHighttime(long highttime) {
        this.highttime = highttime;
    }

    public long getHighttime() {
        return highttime;
    }

    public void setLowtime(long lowtime) {
        this.lowtime = lowtime;
    }

    public long getLowtime() {
        return lowtime;
    }

    public void setClosetime(long closetime) {
        this.closetime = closetime;
    }

    public long getClosetime() {
        return closetime;
    }

    public ITick getOpenTick() {
        return new Tick(time, open, open, 0, 0);
    }

    public ITick getHighTick() {
        return new Tick(highttime, high, high, 0, 0);
    }

    public ITick getLowTick() {
        return new Tick(lowtime, low, low, 0, 0);
    }

    public ITick getCloseTick() {
        return new Tick(closetime, close, close, 0, 0);
    }

    public String toString() {
        return (new Date(time)).toGMTString() + "," + open + "," + high + "," + low + "," + close + "," + volume;
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
